import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(3);
        list.add(4);
        list.add(1,2);
        System.out.println(list);
        LinkedList<Integer> list2 = new LinkedList<>();
        list2.add(5);
        list2.add(6);
        list2.add(7);
        list.addAll(list2);//list2放到list中
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
        for(Integer i : list) {
            System.out.print(i + " ");
        }
        System.out.println();
        Iterator<Integer> iterator = list.iterator();
        while(iterator.hasNext()) {//顺序遍历
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
        ListIterator<Integer> listIterator = list.listIterator(list.size());
        while(listIterator.hasPrevious()) {//逆序遍历
            System.out.print(listIterator.previous() + " ");
        }
        System.out.println();
        list.clear();
    }
}
